import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    {path:'/',
    component:()=>import('@/components/Home.vue'),
    redirect:'/user', 
     children:[
      {path:'user',component:()=>import('@/components/menus/User.vue')},
      {path:'good',component:()=>import('@/components/menus/Good.vue')},
      {path:'order',component:()=>import('@/components/menus/Order.vue')},
      {path:'right',component:()=>import('@/components/menus/Right.vue')},
      {path:'setting',component:()=>import('@/components/menus/Setting.vue')},
      {path:'userdetail/:id',component:()=>import('@/components/user/UserDetail.vue')}
    ]},
    {path:'/login',component:()=>import('@/components/Login.vue')},
    
  ]
})

router.beforeEach((to,from,next)=>{
  if(to.path==='/login'){
    next()
  }else{
    const token = localStorage.getItem('token')
    if(token){
      next()
    }else{
      next('/login')
    }
  }
})
export default router